package com.tc.server.handler;

import com.tc.server.session.SessionFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * @Auther: tianchao
 * @Date: 2022/2/16 15:24
 * @Description:
 */
@Slf4j
public class QuitHandler extends ChannelInboundHandlerAdapter {
    /**
     * 连接断开触发Inactive事件 正常调用ctx.channel().close();
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        String username = SessionFactory.getSession().unbind(ctx.channel());
        log.error("正常断开==用户名{} 已经断开 channel:{}",username,ctx.channel());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        String username = SessionFactory.getSession().unbind(ctx.channel());
        log.error("异常断开==用户名{} 已经断开 channel:{}",username,ctx.channel());
    }
}
